package com.wsz.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.commonutils.JwtUtils;
import com.wsz.commonutils.R;
import com.wsz.eduservice.client.OrderClient;
import com.wsz.eduservice.pojo.Course;
import com.wsz.eduservice.pojo.Report;
import com.wsz.eduservice.pojo.frontvo.CourseFrontVo;
import com.wsz.eduservice.pojo.frontvo.CourseWebVo;
import com.wsz.eduservice.pojo.vo.chapter.ChapterVo;
import com.wsz.eduservice.service.ChapterService;
import com.wsz.eduservice.service.CourseService;
import com.wsz.eduservice.service.ReportService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private ReportService reportService;

    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page,
                                @PathVariable long limit,
                                @RequestBody CourseFrontVo courseFrontVo) {
        Page<Course> pages = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pages,courseFrontVo);

        return R.ok().data(map);
    }

    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){

        CourseWebVo courseWebVo = courseService.getBaseCourseId(courseId);

        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        Boolean buyCourse = false;

        if(courseId == null || memberId == null){
        }else{
           buyCourse = orderClient.isBuyCourse(courseId,memberId);
        }
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }

    @ApiOperation("通过用户Id获取用户购买所有课程")
    @GetMapping("/getBuyCourse/{id}")
    public R getBuyCourse(@PathVariable String id){

        R buyCourse = orderClient.getBuyCourse(id);
        Map<String, Object> data = buyCourse.getData();
        List<String> list = (List<String>)data.get("list");
        List<Course> list1 = courseService.getBuyCourse(list);
        return R.ok().data("buyCourse",list1);
    }

    @ApiOperation("举报")
    @PostMapping("/report")
    public R report(@RequestBody Report report){
        boolean save = reportService.save(report);
        if(save){
            return R.ok();
        }else{
            return R.error().code(20001).message("举报失败");
        }
    }
}
